<template>
	<label :style="componentModel.cssStyle" :class="componentModel.cssClass">{{label}}</label>
</template>

<script>
	import utils from '@/utils/utils.js';
	export default {
		name: "LabelComponent",
		props: ["componentModel","rowData"],
		data() {
			return {
				label: "",
				hasSubmit: false,
				required: false,
				"currentComponentModel":this.componentModel
			}
		},
		inject:['children'],
		mounted() {
			this.children.push(this);
			if (this.componentModel.label != undefined) {
				this.label = this.componentModel.label;
			}
			if (this.componentModel.cssStyle == undefined) {
				this.currentComponentModel.cssStyle = "font-size:14px;";
			}
			if(this.rowData!=undefined){
				var value=this.rowData[this.componentModel.dataField];
				if(value===true){
					this.label =this.componentModel.label+":"+"是";
					return;
				}else if(value===false){
					this.label =this.componentModel.label+":"+"否";
					return;
				}else if(value==null){
					this.label =this.componentModel.label+":"+"";
					return;
				}
				var isProcessed=false;
				var selectItemMap=this.rowData["selectItemMap"];
				if(selectItemMap!=undefined){
					var selectItems=selectItemMap[this.componentModel.id];
					if(selectItems!=undefined){
						if (typeof value == "string") {
							var valueArray = value.split(",");
							if (valueArray.length > 1) {
								var label = "";
								for (var i = 0; i < valueArray.length; i++) {
									if (i > 0) {
										label += ",";
									}
									for (var j = 0; j < selectItems.length; j++) {
										if (selectItems[j].value == valueArray[i]) {
											label += selectItems[j].name;
											break;
										}
									}
								}
								this.label = this.componentModel.label+":"+label;
								return;
							}
						}
						for(i=0;i<selectItems.length;i++){
							if(selectItems[i].value==this.rowData[this.componentModel.dataField]){
								this.label =this.componentModel.label+":"+selectItems[i].name;
								isProcessed=true;
								break;
							}
						}
					}
				}
				if(!isProcessed){
					this.label =this.componentModel.label+":"+this.rowData[this.componentModel.dataField];
				}
			}
		},beforeUnmount() {
			utils.removeArrayElement(this.children,this);
		},
		methods: {
			setProp(prop) {
				for (var k in prop) {
					if (k == "value") {
						this.label = prop[k];
						this.hasSubmit = true;
					} else if (k == "required") {
						this.required = prop[k];
					}else if(k=="label"){
						if(!this.hasSubmit){
							this.label = prop[k];
						}
					}
				}
			},
			getValue() {
				if (!this.hasSubmit) {
					return undefined;
				}
				return this.label;
			}
		},
	}
</script>

<style>
</style>
